package com.github.freewu.commons.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaRecorder;
/**
 * 
* @Description: TODO(音频操作类) 
* @author 唐飞
* @date 2015年3月20日 下午4:06:06 *
 */
public class Audio {
	MediaPlayer mediaPlayer;
	public MediaRecorder mediaRecorder;
	/**开始录音**/
	public boolean startAudio(String path){
		mediaRecorder = new MediaRecorder();
		mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
		mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.RAW_AMR);
		mediaRecorder.setOutputFile(path);
		mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
		try {
			mediaRecorder.prepare();
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		try {

			mediaRecorder.start();
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}
	/**结束录音**/
	public void stopAudio(){
		try {
			if(mediaRecorder != null){
				mediaRecorder.stop();
				mediaRecorder.release();
				mediaRecorder = null;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**播放录音**/
	public void playAudio(Context context, String path){
		mediaPlayer = new MediaPlayer();
		try {
			mediaPlayer.setDataSource(path);
			mediaPlayer.prepare();
			mediaPlayer.start();
			mediaPlayer.setOnCompletionListener(new OnCompletionListener() {				
				@Override
				public void onCompletion(MediaPlayer mp) {
					if(onCompListener != null){
						onCompListener.onFinsh();
					}
				}
			});
		} catch (Exception e) {
			e.printStackTrace();
			if(onCompListener != null){
				onCompListener.onFinsh();
			}
		}
	}
	/**开始播放**/
	public void resumeAudio(){
		if(mediaPlayer != null){
			mediaPlayer.start();
		}
	}
	/**暂停播放**/
	public void pauseAudio(){
		if(mediaPlayer != null){
			mediaPlayer.pause();
		}
	}
	
	/**停止播放录音**/
	public void stopPlayAudio(){
		if(mediaPlayer != null){
			mediaPlayer.release();
			mediaPlayer = null;
		}
	}
	/**获取音频时长**/
	public void getAudioTime(String path){
		mediaPlayer = new MediaPlayer();
		try {
			mediaPlayer.setDataSource(path);
			mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
			mediaPlayer.prepare();
			int duration = mediaPlayer.getDuration();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 定位到播放位置
	 * @param position
	 */
	public void seekTo(int position){
		if(mediaPlayer != null){
			mediaPlayer.seekTo(position);
		}
	}
	/**
	 * 获取当前音频播放位置
	 * @return
	 */
	public int getCurrentPosition(){
		if(mediaPlayer != null){
			return mediaPlayer.getCurrentPosition();
		}
		return 0;
	}
	/**
	 * 获取音频总时间长度
	 * @return
	 */
	public int getDuration(){
		if(mediaPlayer != null){
			return mediaPlayer.getDuration();
		}
		return 0;
	}
	/**
	 * 录音拼接
	 * @param context
	 * @param list
	 * @return
	 */
	public String getInputCollection(Context context, List<String> list) {
		String path = "" + "/" + System.currentTimeMillis() + ".mp3";
		// 创建音频文件,合并的文件放这里
		File bestFile = new File(path);
		FileOutputStream fileOutputStream = null;
		if (!bestFile.exists()) {
			try {
				bestFile.createNewFile();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		try {
			fileOutputStream = new FileOutputStream(bestFile);
		} catch (IOException e) {
			e.printStackTrace();
		}
		for (int i = 0; i < list.size(); i++) {
			File file = new File(list.get(i));
			try {
				FileInputStream fileInputStream = new FileInputStream(file);
				byte[] myByte = new byte[fileInputStream.available()];
				// 文件长度
				int length = myByte.length;
				// 头文件
				if (i == 0) {
					while (fileInputStream.read(myByte) != -1) {
						fileOutputStream.write(myByte, 0, length);
					}
				}else {// 之后的文件，去掉头文件就可以了
					while (fileInputStream.read(myByte) != -1) {
						fileOutputStream.write(myByte, 6, length - 6);
					}
				}

				fileOutputStream.flush();
				fileInputStream.close();
			} catch (Exception e) {
				e.printStackTrace();
				return null;
			}

		}
		// 结束后关闭流
		try {
			fileOutputStream.close();
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
		return path;
	}
	
	OnCompListener onCompListener;
	public void setOnCompListener(OnCompListener onCompListener){
		this.onCompListener = onCompListener;
	}
	public interface OnCompListener{
		public void onFinsh();
	}
}
